﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using FinApp.DataProviders.Configuration;
using FinApp.ViewModels.Base;
using FinApp.Commands.Base;
using Microsoft.Phone.Controls;
using FinApp.DataProviders;
using FinApp.Model;
using System.Device.Location;
using Microsoft.Phone.Tasks;
using System.Linq;

namespace FinApp.ViewModels {
    public class CreditCardSelectorViewModel : ViewModelBase {

        #region Members
        private string commerceId;
        private double quantity;
        private string password;
        private string salt;
        private string checkField;
        #endregion

        #region Constructors
        public CreditCardSelectorViewModel() {
            this.CreditCards = new ObservableCollection<CreditCardViewModel>();
        }
        #endregion

        #region Properties
        /// <summary>
        /// Obtiene si hay datos cargados
        /// </summary>
        public bool IsDataLoaded {
            get;
            private set;
        }
        /// <summary>
        /// Colección para objetos CreditCardViewModel.
        /// </summary>
        public ObservableCollection<CreditCardViewModel> CreditCards { get; private set; }

        public string CommerceId {
            get { return commerceId; }
            set { commerceId = value; }
        }

        public double Quantity {
            get { return quantity; }
            set { quantity = value; }
        }

        public string Password {
            get { return password; }
            set { password = value; }
        }

        public string Salt {
            get { return salt; }
            set { salt = value; }
        }

        public string CheckField {
            get { return checkField; }
            set { checkField = value; }
        }
        #endregion

        #region Overrides
        /// <summary>
        /// Carga de datos de los comercios que ofrecen promociones.
        /// </summary>
        public override void LoadData() {
            CardProvider.Instance.CardCompleted += new DataProviders.EventArguments.CardCompleteEventHandler(OnCardCompleted);
            CardProvider.Instance.GetCardStatus();
        }
        #endregion

        #region Events

        private void OnCardCompleted(object sender, DataProviders.EventArguments.CardEventArgs e) {
            if (e.CardStatusModel != null && e.CardStatusModel.CardPaymentDataModel != null) {
                this.CreditCards.Add(new CreditCardViewModel() { 
                    Id = e.CardStatusModel.Id, 
                    Issuer = e.CardStatusModel.Issuer, 
                    Mode = e.CardStatusModel.Mode, 
                    Balance = (e.CardStatusModel.Mode == "CREDIT") ? e.CardStatusModel.CardPaymentDataModel.CreditAvailable : e.CardStatusModel.CardPaymentDataModel.TotalDebt,
                    CheckField = this.CheckField,
                    CommerceId = this.CommerceId,
                    CommercePasswordKey = this.Password,
                    CommerceSaltKey = this.Salt,
                    Password = this.Password,
                    Salt = this.Salt
                });
                this.IsDataLoaded = true;
            }
        }
        //private void OnCommerceCompleted(object sender, DataProviders.EventArguments.CommerceEventArgs e) {

        //    //foreach (CommerceDataModel commerceItem in e.CommerceDataModel) {
        //    if (e.CommerceDataModel.AddresModel != null) {
        //        string commerceAddres = e.CommerceDataModel.AddresModel.Street + " " + e.CommerceDataModel.AddresModel.Number;
        //        this.CommerceItems.Add(new CommerceItemViewModel() { CommerceName = e.CommerceDataModel.PublicName, CommerceAddres = commerceAddres, CommerceGeoCoordinate = new System.Device.Location.GeoCoordinate(e.CommerceDataModel.Location[0], e.CommerceDataModel.Location[1]) });
        //        //}
        //        this.IsDataLoaded = true;
        //    }
        //}

        //public void OnPositionChanged(object sender, DataProviders.EventArguments.PositionEventArgs e) {
        //    this.CurrentPosition = e.GeoCoordinate;
        //}
        #endregion



    }
}
